package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//反射的Class类的使用；
public class ReflectClassDemo {
    public static void reflectNewInstance(){
        Class<?> stu1;
        try{
            stu1=Class.forName("reflectdemo.Student");
            //进行获取Student这个类，方便进行获取这个类的实例对象以及相关的方法和属性；
            Student student=(Student)stu1.newInstance();
            //进行获取这个对象的实例；
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    //进行反射私有的构造方法；
    public static void reflectPrivateConstructor(){
        Class<?> stu1;
        try{
            stu1=Class.forName("reflectdemo.Student");
            Constructor<?> constructor= stu1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Student student= (Student) constructor.newInstance("zhuangSan",11);
            System.out.println(student);


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    //进行使用反射获取私有属性；
    public static void reflectPrivateFiled() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        Class<?> stu1;
        stu1=Class.forName("reflectdemo.Student");
        Field field= stu1.getDeclaredField("name");
        field.setAccessible(true);
        Student student=(Student)stu1.newInstance();
        //想要通过反射获取对象的属性进行修改，需要明确：
        //1.修改对象的那个字段；
        //2，要修改对象的值是什么；
        field.set(student,"lisi");
        System.out.println(student);
    }
    //通过反射进行获取这个类的私有方法；
    public static void reflectPrivateMethod() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        Class<?> stu1;
        stu1=Class.forName("reflectdemo.Student");
        Method method= stu1.getDeclaredMethod("function", String.class);
        Student student = (Student)stu1.newInstance();
        method.setAccessible(true);
        //进行获取method方法的秘钥管理控制
        //进行通过反射进行使用内部方法的构造与使用；
        method.invoke(student,"我试一试进行传入一个参数进行相关操作");

    }
    public static void main(String[] args) throws NoSuchFieldException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        //reflectNewInstance();
        //进行获取这个类；以及进行获取这个对象的实例；
        //reflectPrivateConstructor();
        //进行获取这个对象的构造方法；
       // reflectPrivateFiled();
        reflectPrivateMethod();

    }

}
